package servlets;

import entities.Admin;
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sessions.IUserAccountSession;
import util.BreadCrumbItem;
import util.Script;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@WebServlet("/addlocation.html")
public class AddLocationServlet extends ServletBase {
    private final static BreadCrumbItem[] BREAD_CRUMB_ITEMS = {
        new BreadCrumbItem("/Trabalho_PD/homepage.html", "Homepage", false),
        new BreadCrumbItem("/Trabalho_PD/serverconfigurations.html", "Server configurations", false),
        new BreadCrumbItem("/Trabalho_PD/addlocation.html", "Add location", true)};
    private final static Script[] EXTRA_SCRIPTS = {
        new Script("resources/js/webservicegooglegeo.js", true),
        new Script("resources/js/webserviceboleias.js", true),
        new Script("resources/js/customjs/addlocation.js", true),
        new Script("http://maps.googleapis.com/maps/api/js?key=AIzaSyD46u-OiCXf9xFnMI8pTaJ1R6LevKdMmRM&sensor=false", false)};
    
    @Inject private IUserAccountSession userAccountSession;
    
    public AddLocationServlet() {
        super("Add location", BREAD_CRUMB_ITEMS, null, EXTRA_SCRIPTS);
    }

    @Override
    protected void onGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (userAccountSession.isVisitor()) {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
            return;
        }
        Admin admin = userAccountSession.getAdmin();
        if (admin != null) {
            callJsp(JSP_ADD_LOCATION, req, resp);
        } else {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
        }
    }

    @Override
    protected void onPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (userAccountSession.isVisitor()) {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
            return;
        }
        Admin admin = userAccountSession.getAdmin();
        if (admin != null) {
            callJsp(JSP_ADD_LOCATION, req, resp);
        } else {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
        }
    }
}
